﻿import QtQuick
import QtQuick.Controls
import QtQuick.Dialogs
import QtQuick.Layouts
import cpp.qml 1.0

Window {
    visible: true
    width: 800
    height: 600

    PdfUtil {
        objectName: "pdfObj"
        id: pdfUtil
        onImgMsgChanged: {
            console.log("load success");
        }
    }

    property int updateNum: 0
    // Connections方式连接信号
    Connections {
        target: pdfImage
        function onImageChanged(imageTag) {
            // Image 的 load 接口不是 public 的，所以只能切换 url 来重新加载数据
            updateNum++
            image.source = "image://provider/" + imageTag + "/" + updateNum
        }
    }

    FileDialog {
        id: fileDialog
        title: "请选择PDF文件"
        nameFilters: ["文本文件 (*.pdf)", "所有文件 (*)"]
        onAccepted: {
            var filePath = fileDialog.selectedFile.toString().replace("file:///","")
            console.log("选择的文件: " + filePath)
            // 处理选中的文件
            textFieldPdf.text = filePath
            pdfUtil.open(filePath, image.width, image.height)
        }
        onRejected: {
            console.log("选择被取消")
        }
    }

    DropArea {
        anchors.fill: parent
        onEntered: {
            if (drag.urls.length !== 1) {
                // 这里只接收一个文件
                drag.accepted = false
                return false
            }
        }
        onDropped: {
            var urls = drop.urls;
            for (var i = 0; i < urls.length; ++i) {
                var filePath = urls[i].toString().replace("file:///","")
                textFieldPdf.text = filePath
                console.log("Dropped file:", filePath)
                // 在这里处理文件路径，例如打开文件或执行其他操作
                pdfUtil.open(filePath, image.width, image.height)
            }
        }
    }

    StackLayout {
        anchors.fill: parent
        currentIndex: 1

        Rectangle {

            RowLayout {
                id: head
                layoutDirection: Qt.LeftToRight
                anchors.left: parent.left
                anchors.right: parent.right
                spacing: 10
                TextField {
                    id: textFieldPdf
                    Layout.fillWidth: true
                    Layout.horizontalStretchFactor: 1 // 加fill 按比例 和期望选其一
                    Layout.preferredWidth: 300 // 期望的宽度
                    Layout.margins: 10
                    placeholderText: "点击选择按钮获取PDF路径，或直接拖拽进来"
                }
                Button {
                    text: "选择"
                    onClicked: {
                        fileDialog.open()
                    }
                }
                Button {
                    text: "截取"
                    onClicked: {
                        // 控件中间坐标
                        console.log("item.width = " + item.width + ", item.height = " + item.height)
                        console.log("image.width = " + image.width + ", image.height = " + image.height)
                        console.log("image.x = " + image.x + ", image.y = " + image.y)
                        console.log("item.x = " + item.x + ", item.y = " + item.y)
                        // 截取图片的坐标
                        var x = rectangle.x - image.x
                        var y = rectangle.y - image.y
                        pdfUtil.clipImg(x, y, rectangle.width)
                    }
                }
                Text {
                    Layout.fillWidth: true
                    Layout.horizontalStretchFactor: 1
                }
            }

            Item {
                id: item
                anchors.top: head.bottom
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                anchors.leftMargin: 10
                anchors.rightMargin: 10
                anchors.bottomMargin: 10

                Image {
                    id: image
                    // source: "image://provider/imageTag/0"
                    // fillMode: Image.PreserveAspectFit

                    MouseArea {
                        anchors.fill: parent
                        property point clickLocation: "0,0"
                        // 鼠标移入事件，默认关闭
                        // hoverEnabled: true
                        acceptedButtons: Qt.AllButtons
                        drag.target: parent
                        onPressed: (mouse) => {
                            // 记录鼠标点击的位置
                            clickLocation = { x: mouse.x, y: mouse.y }
                            console.log("onPressed---> " + mouse.x + " " + mouse.y + " img " + image.x)
                        }
                        onReleased:
                            (mouse) => {
                                console.log("onReleased---> " + mouse.x + " " + mouse.y + " img " + image.x)
                            }

                        onPositionChanged:
                            (mouse) => {
                               // 拖拽处理
                               console.log("onPositionChanged---> " + mouse.x + " " + mouse.y)
                               //image.x += (mouse.x - clickLocation.x)
                               //image.y += (mouse.y - clickLocation.y)
                               // 更新点击位置
                               clickLocation = { x: mouse.x, y: mouse.y }
                        }

                        // 缩放处理
                        onWheel:
                            (wheel) => {
                                // 缩放比例 1.1 代表放大10%，0.9 代表缩小10%
                                //var scale = wheel.angleDelta.y > 0 ? 1.1 : 0.9
                                //image.scale = image.scale * scale
                        }
                    }
                }

                Rectangle {
                    id: rectangle
                    property int r_w: 50
                    x: item.width / 2.0 - r_w/2
                    y: item.height / 2.0 - r_w/2
                    implicitWidth: r_w
                    implicitHeight: r_w
                    color: "transparent"
                    border.color: "#AA4169F1"
                    border.width: 3
                    MouseArea {
                        anchors.fill: parent
                        // 缩放处理
                        onWheel:
                            (wheel) => {
                                if (wheel.angleDelta.y > 0) {
                                    rectangle.width+=2
                                    rectangle.height+=2
                                    rectangle.x--
                                    rectangle.y--
                                } else {
                                    rectangle.width-=2
                                    rectangle.height-=2
                                    rectangle.x++
                                    rectangle.y++
                                }
                        }
                    }
                }

            }

        }

        Rectangle {
            List {
                anchors.fill: parent
            }
        }
    }
}
